package com.example.springboot2.oa.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.springboot2.base.web.BaseController;
import com.example.springboot2.oa.entity.TMenu;
import com.example.springboot2.oa.entity.TUser;
import com.example.springboot2.oa.service.ITMenuService;
import com.example.springboot2.tools.MakeResponse;
import com.example.springboot2.tools.ResponseMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.function.Consumer;

@CrossOrigin
@RestController
@RequestMapping("/oa/t-menu")
public class TMenuController extends BaseController {
    @Autowired
    private ITMenuService menuService;

    @GetMapping("powerByRole")
    public ResponseMessage powerByRole(String token,
                                       String roleId,
                                       Integer status,
                                       HttpServletRequest request) {
        ResponseMessage rsp = authentication(token);
        if (rsp != null) {
            return rsp;
        }

        List<TMenu> data = menuService.powerByRole(status, roleId);
        return MakeResponse.success("200", "角色权限列表", data);
    }

    @GetMapping("getMeunListByRole")
    public ResponseMessage getMeunListByRole(HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        TUser user = getUser(request);
        if (user == null) {
            return MakeResponse.success("401", "无权限访问");
        }

        List<TMenu> data = menuService.powerByRole(1, user.getRoleId());
        return MakeResponse.success("200", "角色权限列表", data);
    }

    @PostMapping("listAll")
    public ResponseMessage listAll(@RequestBody TMenu menu, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc("menu_id");
        List<TMenu> data = menuService.list(queryWrapper);
        return MakeResponse.success("200", "菜单列表", data);
    }

    @PostMapping("add")
    public ResponseMessage add(@RequestBody TMenu menu, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_name", menu.getMenuName());
        if (menuService.getOne(wrapper) != null) {
            return MakeResponse.success("500", "已经存在");
        }

        if (!menuService.save(menu)) {
            return MakeResponse.success("500", "添加失败");
        }

        return MakeResponse.success("200", "添加成功");
    }

    @PostMapping("modify")
    public ResponseMessage modify(@RequestBody TMenu menu, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        QueryWrapper<TMenu> wrapper = new QueryWrapper<>();
        wrapper.eq("menu_id", menu.getMenuId());
        if (!menuService.update(menu, wrapper)) {
            return MakeResponse.success("500", "更新失败");
        }

        return MakeResponse.success("200", "更新成功");
    }

    @PostMapping("delete")
    public ResponseMessage delete(@RequestBody List<TMenu> menuList, HttpServletRequest request) {
        ResponseMessage rsp = authentication(request);
        if (rsp != null) {
            return rsp;
        }

        List<Integer> idList = new ArrayList<>();
        menuList.forEach(new Consumer<TMenu>() {
            @Override
            public void accept(TMenu menu) {
                idList.add(menu.getMenuId());
            }
        });

        QueryWrapper<TMenu> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.in("menu_id", idList);
        if (!menuService.remove(deleteWrapper)) {
            return MakeResponse.success("500", "删除失败");
        }

        return MakeResponse.success("200", "删除成功");
    }
}
